<?php
namespace Rainrock\Framework\Controller;


use Rainrock\Framework\kernel\core\Controller;
use Rainrock\Framework\kernel\base\Request;
use Rainrock\Framework\kernel\base\File;
use Rainrock\Framework\kernel\base\Rock;
use Rainrock\Framework\kernel\base\CLog;

/**
*	静态文件读取
*/
class WebstaticController extends Controller{
	
	public function indexAction()
	{
		$file = Request::get('file');
		$type = File::getExt($file);
		if($type=='js')$this->setContenttype('application/javascript; charset=UTF-8');
		if($type=='css')$this->setContenttype('text/css; charset=UTF-8');
		$path = ''.ROOT_PATM.'/vendor/rainrock/framework/src/webstatic/'.$file.'';
		if(file_exists($path)){
			return file_get_contents($path);
		}else{
			$this->addHeader('HTTP/1.1 404 Not Found');
			return '404 Not Found';
		}
	}
	
	/**
	*	更新下载前端文件
	*/
	public function updateAction()
	{
		$path		= ''.ROOT_PATM.'/vendor/rainrock/framework/src/webstatic';
		$this->webstatic($path, '');
		CLog::show('--- webstatic update success ---');
	}
	private function webstatic($path, $luji)
	{
		$files		= File::getFolder($path);
		foreach($files as $file){
			if($this->contain($file, '.')){
				$bool = Rock::createFile('webstatic/'.$luji.''.$file.'', file_get_contents($path.'/'.$file), ROOT_PATH);
				if($bool)CLog::show('--- webstatic/'.$luji.''.$file.' ---');
			}else{
				$this->webstatic($path.'/'.$luji.''.$file, ''.$luji.''.$file.'/');
			}
		}
	}
	
}